package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.CommonResult;
import com.atguigu.eduservice.entity.CrmBanner;
import com.atguigu.eduservice.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author 李红磊
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;


    //1.banner的分页查询
    @ApiOperation(value = "banner的分页查询")
    @GetMapping("pageBanner/{page}/{limit}")
    public CommonResult pageBanner(@PathVariable("page") Long page,
                                   @PathVariable("limit") Long limit) {
        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        crmBannerService.page(pageBanner, null);

        return CommonResult.ok().data("items", pageBanner.getRecords()).data("total", pageBanner.getTotal());
    }


    //添加banner
    @ApiOperation(value = "添加banner")
    @PostMapping("addBanner")
    public CommonResult addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return CommonResult.ok();
    }

    //根据id获取Banner
    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public CommonResult get(@PathVariable String id) {
        CrmBanner banner = crmBannerService.getById(id);
        return CommonResult.ok().data("item", banner);
    }

    //修改Banner
    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public CommonResult updateById(@RequestBody CrmBanner banner) {
        crmBannerService.updateById(banner);
        return CommonResult.ok();
    }

    //根据id删除Banner
    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public CommonResult remove(@PathVariable String id) {
        crmBannerService.removeById(id);
        return CommonResult.ok();
    }


}

